﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FRL.DebtChased.Repository
{

	/// <summary>
	/// Declaration of ErrorProcessing class
	/// </summary>
	public static class ErrorProcessing
	{

		/////////////////////////////////////////////////////////////
		// Static operations

		public static string HandleException(Exception x, string strFailedTo, int? iUserId)
		{

			// Declare return variable
			string strFtmp;

			// Declare a repository
			using (DebtChasedEntities ent = new DebtChasedEntities())
			{

				// Show full error
				bool bShowFullError = false;
				if (iUserId == null)
					bShowFullError = true;
				else
				{

					// Get the roles for the user
					Definitions.eRoles roles = User.GetUserRoles(ent, iUserId.Value);
					if ((roles & Definitions.eRoles.FIN_SYSADMIN) == Definitions.eRoles.FIN_SYSADMIN)
						bShowFullError = true;

				}

				// If showing full error...
				if (bShowFullError)
				{

					// Show the full error
					if (x.InnerException != null)
						strFtmp = string.Format("Failed to {0}.  {1}.  Inner exception: {2}", strFailedTo, x.Message, x.InnerException);
					else
						strFtmp = string.Format("Failed to {0}.  {1}", strFailedTo, x.Message);

				}
				else
				{

					// Show helf of the error
					strFtmp = string.Format("Failed to {0}.  {1}", strFailedTo, Definitions.SeeErrorLog);

				}

				// Load the error
				string strAction2 = string.Format("{0}{1}",
					char.ToUpper(strFailedTo[0]),
					strFailedTo.Substring(1)
				);
				ErrorLog.Add(ent, strAction2, x, iUserId);

			}

			// Return variable
			return strFtmp;

		}

	}

}
